package logic

import (
	"context"
	"fmt"
	"house/models"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善信息
func (l *UserPerfectLogic) UserPerfect(in *houserpc.UserPerfectRequest) (*houserpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User
	models.DB.Debug().Where("account = ?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	user = models.User{
		Model:    gorm.Model{ID: uint(in.UserId)},
		Account:  in.Account,
		Password: in.Password,
		Image:    in.Image,
		Name:     in.Name,
	}
	models.DB.Debug().Updates(&user)

	return &houserpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
